package service.sys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.common.BaseMapper;
import com.common.BaseService;
import com.common.Cache;
import com.common.CacheManager;

import mapper.sys.FunMapper;
import mapper.sys.VUserFunMapper;

@Service
@Transactional
public class FunServiceImpl<T> extends BaseService<T>{

	@Autowired
	private FunMapper<T> funMapper;

	@Autowired
	private VUserFunMapper userFunMapper;

	/*
	 * 根据用户获取拥有的菜单
	 */
	public List<Map> getAll(String uid){
		if(uid!=null&&!uid.equals("")){
			
			Cache cache = CacheManager.getCacheInfo("fun-"+uid);
			List<Map> m = null;
			if(cache!=null) m = (List<Map>)cache.getValue();
			if(m==null){
				m = userFunMapper.getByUserid(uid);
				cache = new Cache();
				cache.setValue(m);
				CacheManager.putCache("fun-"+uid, cache);
			}
			
			return m;
		}
		else{
			List<Map> m = userFunMapper.getAll();
			return m;
		}
	}
	
	public List<Map> getByPid(String pid, String title, String linkurl){
		List<Map> m = userFunMapper.getByPid(pid,title,linkurl);
		return m;
	}
	
	public void deletes(Object...ids){
		List<String> list = new ArrayList<>();
		for(Object id : ids ){
			list.add(id.toString());
		}
		funMapper.batchDeletes(list);
	}

	@Override
	public BaseMapper<T> getMapper() {
		return funMapper;
	}
}